# -*- coding: utf-8 -*-
from django.db import models
from django.contrib.auth.models import User

class CodeLanguage(models.Model):
    name = models.CharField(max_length=100)
    
    def __unicode__(self):
        return unicode(self.name)


class UserRating(models.Model):
    user = models.ForeignKey(User)
    score = models.IntegerField()    
    code = models.ForeignKey('Code')
    
    def __unicode__(self):
        return u"%s's rating of %s for %s" % (self.user.username, self.score, self.code.title)

class Project(models.Model):
    user = models.ForeignKey(User)
    title = models.CharField(max_length=200)
    summary = models.CharField(max_length=400)
    
    def __unicode__(self):
        return unicode(self.name)
    
class Code(models.Model):
    author = models.ForeignKey(User)
    title = models.CharField(max_length=200, null=True, blank=True)
    description = models.TextField(max_length=600, null=True, blank=True)
    created = models.DateTimeField(auto_now_add=True)
    edited = models.DateTimeField(null=True, blank=True)
    code = models.TextField()
    language = models.ForeignKey(CodeLanguage)
    average = models.FloatField(verbose_name="Average Rating", default=0)
    views = models.IntegerField(default=0)
    downloads = models.IntegerField(default=0)
    project = models.ForeignKey("Project", null=True, blank=True)
    
    def __unicode__(self):
        return u"%s (%s)" % (self.title, self.language.name)
    
    def get_absolute_url(self):
        return '/%s/' % self.pk
